package com.primb.bench.project.home.presenter;

import android.content.Context;
import android.view.View;

import com.primb.bench.R;
import com.primb.bench.common.override.HttpSubscriber;
import com.primb.bench.project.home.activity.PromiseApplyActivity;
import com.primb.bench.project.home.contract.PromiseApplyContract;
import com.primb.bench.project.home.entity.HttpProInfo;
import com.primb.bench.project.home.entity.HttpPromiseEntity;
import com.primb.bench.project.home.entity.LoanCalculateInfoEntity;
import com.primb.bench.project.home.entity.PromiseApplyEntity;
import com.primb.bench.project.home.model.PromiseApplyModelImpl;
import com.primb.netlibs.exception.ApiException;

import rx.Subscription;
import rx.subscriptions.CompositeSubscription;

/**
 * Created by Administrator on 2017/05/18
 */

public class PromiseApplyPresenterImpl implements PromiseApplyContract.Presenter {

    private PromiseApplyModelImpl mModel;
    private PromiseApplyContract.View mView;
    private CompositeSubscription subscription;
    private LoanCalculateInfoEntity infoEntity;
    private Context context;

    public PromiseApplyPresenterImpl(Context context, PromiseApplyContract.View mView, LoanCalculateInfoEntity infoEntity) {
        this.mView = mView;
        this.context = context;
        this.infoEntity = infoEntity;
        this.mModel = new PromiseApplyModelImpl();
        subscription = new CompositeSubscription();
    }

    @Override
    public void start() {
    }

    @Override
    public void onRecycle() {
        subscription.clear();
    }

    @Override
    public void onViewClick(View view) {
        switch (view.getId()) {
            case R.id.btn_promise_reset:
                mView.resetView();
                break;
            case R.id.btn_promise_calculate:
                PromiseApplyEntity applyEntity = mView.getEntity();
                if (((int) view.getTag()) == PromiseApplyActivity.BUTTON_CALCULATE && !checkEntityIsNull(applyEntity)) {
                    infoEntity.setPromiseApplyEntity(applyEntity);
                    calculateGoodRate(infoEntity);
                    return;
                } else if (((int) view.getTag()) == PromiseApplyActivity.BUTTON_SAVE) {
                    applyEntity.setPromiseDetail(true);
                    mView.setResultToCalculate();
                } else {
                    mView.showError("请完善信息后重试");
                }
                break;
            case R.id.tv_promise_start_date:
                mView.showDatePicker(0);
                break;
            case R.id.tv_promise_end_date:
                String promiseStartDate = infoEntity.getPromiseApplyEntity().getPromiseStartDate();
                if (promiseStartDate == null || promiseStartDate.equals("")) {
                    mView.showError("请先选择起始日");
                    return;
                }
                mView.showDatePicker(1);
                break;
        }
    }

    @Override
    public boolean checkEntityIsNull(PromiseApplyEntity entity) {
        if (entity.getPromiseDepositDay() != null
                && !entity.getPromiseDepositDay().isEmpty()
                && entity.getPromiseDepositDayNow() != null
                && !entity.getPromiseDepositDayNow().isEmpty()
                && entity.getPromiseDepositSum() != null
                && !entity.getPromiseDepositSum().isEmpty()
                && entity.getPromiseDepositSumNow() != null
                && !entity.getPromiseDepositSumNow().isEmpty()
                && entity.getPromiseEndDate() != null
                && !entity.getPromiseEndDate().isEmpty()
                && entity.getPromiseStartDate() != null
                && !entity.getPromiseStartDate().isEmpty()
//                && entity.getPromiseOtherScene() != null
//                && !entity.getPromiseOtherScene().isEmpty()
                ) {
            return false;
        }
        return true;
    }

    @Override
    public void calculateGoodRate(LoanCalculateInfoEntity infoEntity) {
        mView.showLoading();
        Subscription subscribe = mModel.getCalculateGoodRate(infoEntity).subscribe(new HttpSubscriber<HttpPromiseEntity>(context) {
            @Override
            public void onCompleted() {
                mView.hideLoading();
            }

            @Override
            public void onNext(HttpPromiseEntity entity) {
                if (entity.isSuccess()) {
                    infoEntity.getPromiseApplyEntity().setPromiseFavorableRate(entity.getData().getLnpSceneRate());
                    infoEntity.getPromiseApplyEntity().setPromisePoint(entity.getData().getAvailScore());
                    mView.showCalculateResult(infoEntity.getPromiseApplyEntity());
                }
            }

            @Override
            public void onError(ApiException e) {
                mView.hideLoading();
                mView.showError(e.getMessage());
            }
        });
        subscription.add(subscribe);
    }

    @Override
    public void getPromiseInfo(String custId) {
        mView.showLoading();
        Subscription subscribe = mModel.getPromiseInfo(custId).subscribe(new HttpSubscriber<HttpProInfo>(context) {
            @Override
            public void onCompleted() {
                mView.hideLoading();
            }

            @Override
            public void onNext(HttpProInfo o) {
                if (o.isSuccess()) {
                    mView.showInfo(o.getData().getNowDpMonAvg(), o.getData().getNowDpMonBal());
                    infoEntity.getPromiseApplyEntity().setPromiseDepositDayNow(o.getData().getNowDpMonAvg());
                    infoEntity.getPromiseApplyEntity().setPromiseDepositSumNow(o.getData().getNowDpMonBal());
                } else {
                    mView.hideInfo();
                }
            }

            @Override
            public void onError(ApiException e) {
                mView.hideLoading();
                mView.hideInfo();
            }
        });
        subscription.add(subscribe);
    }
}